package com.cy.jxc.mycache;

import com.cy.jxc.utils.ApplicationContextUtils;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializers;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.DigestUtils;

import java.util.Collection;
import java.util.Set;

public class RedisCache<K,V> implements Cache<K, V> {
    private String cachename;

    public RedisCache(String cachename) {
        this.cachename = cachename;
    }
    public static RedisTemplate getredisTemplate(){
        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return  redisTemplate;
    }
    @Override
    public V get(K k) throws CacheException {

        V o = (V) getredisTemplate().opsForHash().get(this.cachename, k.toString());
        System.out.println("get redis:"+o);
        return o;
    }

    @Override
    public V put(K k, V v) throws CacheException {

        System.out.println("put key"+k);
        System.out.println("put value"+v);
        getredisTemplate().setKeySerializer(new StringRedisSerializer());
        getredisTemplate().setHashKeySerializer(new StringRedisSerializer());
        getredisTemplate().opsForHash().put(this.cachename,k.toString(),v);

        return null;
    }

    @Override
    public V remove(K k) throws CacheException {
        System.out.println("remove  reids"+this.cachename);
        return (V) getredisTemplate().opsForHash().delete(this.cachename,k.toString());
    }

    @Override
    public void clear() throws CacheException {
        getredisTemplate().delete(this.cachename);
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Set<K> keys() {
        return null;
    }

    @Override
    public Collection<V> values() {
        return null;
    }
}
